#include common_scripts\utility;
#include maps\mp\_utility;
#include maps\mp\gametypes\_hud_util;
#include z_mod\_utility;
#using_animtree("multiplayer");
//#using_animtree("vehicles");


init()
{
    precacheModel("weapon_claymore_bombsquad");
    precacheModel("weapon_c4_bombsquad");
    precacheModel("projectile_m67fraggrenade_bombsquad");
    precacheModel("projectile_semtex_grenade_bombsquad");
    precacheModel("weapon_light_stick_tactical_bombsquad");
    
    level waittill("z_run_threads");
    
    level thread onPlayerConnected();
}

onPlayerConnected()
{
    for(;;) {
        level waittill("connected", player);
        
        //player thread onPlayerDied();
        //player thread onPlayerJoinedSpectators();
        //player thread onPlayerJoinedTeam();
        player thread onPlayerSpawned();
        
        player notifyOnPlayerCommand("zkey_centerview", "centerview"); // ufo mode
        player notifyOnPlayerCommand("zkey_mlook", "+mlook"); // ghost mode
        player notifyOnPlayerCommand("zkey_talk", "+talk");
    }
}

onPlayerSpawned()
{
    self endon("disconnect");
    
    for(;;) {
        self waittill("spawned_player");
        
        self thread position_and_ammo();
        self thread ufo_mode();
        self thread ghost_mode();
    }
}

position_and_ammo()
{
    self endon("death");
    self endon("disconnect");
    
    for(;;) {
        self waittill("zkey_talk");
        self sayAll("(" + int(self.origin[0]) + ", " + int(self.origin[1]) + ", " + int(self.origin[2]) + ") @ " + int(self.angles[1]) + " deg");
        self maps\mp\killstreaks\_airdrop::refillAmmo();
        
        level notify("something");
        //self shellShock("pain", 10);
    }
}

ufo_mode()
{
    self endon("death");
    self endon("disconnect");
    
    for(;;) {
        self waittill("zkey_centerview");
        self allowSpectateTeam("freelook", true);
        self.sessionstate = "spectator";
        self setContents(0);
        self iPrintLnBold("^3UFO ^7Mode: ^2ON");
        
        self waittill("zkey_centerview");
        self.sessionstate = "playing";
        self allowSpectateTeam("freelook", false);
        self setContents(100);
        self iPrintLnBold("^3UFO ^7Mode: ^1OFF");
    }
}

ghost_mode()
{
    self endon("death");
    self endon("disconnect");
    
    if(isDefined(self.zTest_Ufo))
        self.zTest_Ufo delete();
    
    if(isDefined(self.zTest_UfoVector))
        self.zTest_UfoVector delete();
    
    for(;;) {
        self waittill("zkey_mlook");
        self.zTest_Ufo = spawn("script_model", self.origin);
        self.zTest_Ufo setModel("weapon_c4_bombsquad");
        self.zTest_Ufo enableLinkTo();
        self.zTest_UfoVector = spawn("script_origin", self.origin);
        self.zTest_UfoVector linkTo(self, "tag_weapon_right", (600, 0, 0), (0, 0, 0));
        
        self playerLinkToDelta(self.zTest_Ufo, "tag_origin", 0, 180, 180, 180, 180, false);
        self thread ghost_mode_moving();
        self iPrintLnBold("^3Ghost ^7Mode: ^2ON");
        
        
        self waittill("zkey_mlook");
        self unLink();
        self.zTest_UfoVector delete();
        self.zTest_Ufo delete();
        self iPrintLnBold("^3Ghost ^7Mode: ^1OFF");
    }
}

ghost_mode_moving()
{
    self endon("death");
    self endon("disconnect");
    self endon("zkey_centerview");
    
    for(;;) {
        if(self playerADS() > 0.95)
            self.zTest_Ufo moveTo(self.zTest_UfoVector.origin, distance(self.zTest_Ufo.origin, self.zTest_UfoVector.origin)/1200);
        else
            self.zTest_Ufo moveTo(self.zTest_Ufo.origin, 0.01);
        
        wait(0.05);
    }
}

